/**
 *
 */
"use strict"
const utils = require('../utils/index'),
mw = require('../middlewares/index.js');
const Trd = require('../models/trd.js');
module.exports.init = router => {
  // router.post('/drafts', mw.verify_token, create);
  // router.patch('/drafts/:id', mw.verify_token, modify)
  // router.delete('/drafts/:id', mw.verify_token,deleteDraft)

  utils.print("trd-init");
  router.get('/trd', trdList);
  router.get('/trd/:employee_id', trdOneList)
  router.get('/trd/:employee_id/:time', trdOneTimeList)

  router.get('/trdStatistics/:employee_id', trdStatistics);
}
function* trdStatistics(){
  var self = this;
  const employee_id = this.params.employee_id;
  const trd = yield Trd.aggregate([{$match:{employee_id:employee_id}},{$group:{_id:{merchant_pid:"$merchant_pid",merchant_name:"$merchant_name"},trd_amt:{$sum:"$trd_amt_1d"}}}]
  ).exec(function(err,docs){
    self.status = 200;
    self.body = {
      success:true,
      data:docs
    }
  });
}
function* trdList(){
  const trd = yield Trd.find()
    .sort({"time":-1})
    .exec().catch(err => {
      utils.logger.error(err);
      this.throw(500,'内部错误')
    });
  this.status = 200;
  this.body = {
    success:true,
    data:trd
  }
}
function* trdOneList(){
  const employee_id = this.params.employee_id;
  const trd = yield Trd.find({employee_id:employee_id})
    .sort({"time":-1})
    .exec().catch(err => {
      utils.logger.error(err);
      this.throw(500,'内部错误')
    });
  this.status = 200;
  this.body = {
    success:true,
    data:trd
  }
}

function* trdOneTimeList(){
  const employee_id = this.params.employee_id;
  const time = this.params.time;
  const trd = yield Trd.find({employee_id:employee_id,time:time})
    .exec().catch(err => {
      utils.logger.error(err);
      this.throw(500,'内部错误')
    });
  this.status = 200;
  this.body = {
    success:true,
    data:trd
  }
}
